{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# numpy.array中的运算\n",
    "## 向量每一个数乘以2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "n =10 \n",
    "L = [ i for i in range(n)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
     },
     "metadata": {},
     "execution_count": 4
    }
   ],
   "source": [
    "2 * L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]"
     },
     "metadata": {},
     "execution_count": 6
    }
   ],
   "source": [
    "A = []\n",
    "for e in L:\n",
    "    A.append(2*e)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "n =1000000\n",
    "L = [ i for i in range(n)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "Wall time: 165 ms\n"
    }
   ],
   "source": [
    "%%time\n",
    "A = []\n",
    "for e in L:\n",
    "    A.append(2*e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "Wall time: 88.3 ms\n"
    }
   ],
   "source": [
    "%%time\n",
    "A = [2*e for e in L]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "L =np.arange(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "Wall time: 0 ns\n"
    }
   ],
   "source": [
    "%%time\n",
    "A = np.array(2*e for e in L)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "Wall time: 15 ms\n"
    }
   ],
   "source": [
    "%%time\n",
    "A = 2* L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([      0,       2,       4, ..., 1999994, 1999996, 1999998])"
     },
     "metadata": {},
     "execution_count": 13
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])"
     },
     "metadata": {},
     "execution_count": 14
    }
   ],
   "source": [
    "n = 10 \n",
    "L = np.arange(n)\n",
    "2 * L"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## universal Functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[ 1,  2,  3,  4,  5],\n       [ 6,  7,  8,  9, 10],\n       [11, 12, 13, 14, 15]])"
     },
     "metadata": {},
     "execution_count": 15
    }
   ],
   "source": [
    "X = np.arange(1, 16).reshape(3,5)\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[ 2,  3,  4,  5,  6],\n       [ 7,  8,  9, 10, 11],\n       [12, 13, 14, 15, 16]])"
     },
     "metadata": {},
     "execution_count": 16
    }
   ],
   "source": [
    "X + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[ 0,  1,  2,  3,  4],\n       [ 5,  6,  7,  8,  9],\n       [10, 11, 12, 13, 14]])"
     },
     "metadata": {},
     "execution_count": 17
    }
   ],
   "source": [
    "X - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[ 2,  4,  6,  8, 10],\n       [12, 14, 16, 18, 20],\n       [22, 24, 26, 28, 30]])"
     },
     "metadata": {},
     "execution_count": 18
    }
   ],
   "source": [
    "X * 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0.5, 1. , 1.5, 2. , 2.5],\n       [3. , 3.5, 4. , 4.5, 5. ],\n       [5.5, 6. , 6.5, 7. , 7.5]])"
     },
     "metadata": {},
     "execution_count": 19
    }
   ],
   "source": [
    "X / 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0, 1, 1, 2, 2],\n       [3, 3, 4, 4, 5],\n       [5, 6, 6, 7, 7]], dtype=int32)"
     },
     "metadata": {},
     "execution_count": 20
    }
   ],
   "source": [
    "X // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[  1,   4,   9,  16,  25],\n       [ 36,  49,  64,  81, 100],\n       [121, 144, 169, 196, 225]], dtype=int32)"
     },
     "metadata": {},
     "execution_count": 21
    }
   ],
   "source": [
    "X ** 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[1, 0, 1, 0, 1],\n       [0, 1, 0, 1, 0],\n       [1, 0, 1, 0, 1]], dtype=int32)"
     },
     "metadata": {},
     "execution_count": 22
    }
   ],
   "source": [
    "X %2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[1.        , 0.5       , 0.33333333, 0.25      , 0.2       ],\n       [0.16666667, 0.14285714, 0.125     , 0.11111111, 0.1       ],\n       [0.09090909, 0.08333333, 0.07692308, 0.07142857, 0.06666667]])"
     },
     "metadata": {},
     "execution_count": 23
    }
   ],
   "source": [
    "1 / X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[ 1,  2,  3,  4,  5],\n       [ 6,  7,  8,  9, 10],\n       [11, 12, 13, 14, 15]])"
     },
     "metadata": {},
     "execution_count": 24
    }
   ],
   "source": [
    "np.abs(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[ 0.84147098,  0.90929743,  0.14112001, -0.7568025 , -0.95892427],\n       [-0.2794155 ,  0.6569866 ,  0.98935825,  0.41211849, -0.54402111],\n       [-0.99999021, -0.53657292,  0.42016704,  0.99060736,  0.65028784]])"
     },
     "metadata": {},
     "execution_count": 25
    }
   ],
   "source": [
    "np.sin(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[ 1.55740772e+00, -2.18503986e+00, -1.42546543e-01,\n         1.15782128e+00, -3.38051501e+00],\n       [-2.91006191e-01,  8.71447983e-01, -6.79971146e+00,\n        -4.52315659e-01,  6.48360827e-01],\n       [-2.25950846e+02, -6.35859929e-01,  4.63021133e-01,\n         7.24460662e+00, -8.55993401e-01]])"
     },
     "metadata": {},
     "execution_count": 26
    }
   ],
   "source": [
    "np.tan(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[2.71828183e+00, 7.38905610e+00, 2.00855369e+01, 5.45981500e+01,\n        1.48413159e+02],\n       [4.03428793e+02, 1.09663316e+03, 2.98095799e+03, 8.10308393e+03,\n        2.20264658e+04],\n       [5.98741417e+04, 1.62754791e+05, 4.42413392e+05, 1.20260428e+06,\n        3.26901737e+06]])"
     },
     "metadata": {},
     "execution_count": 27
    }
   ],
   "source": [
    "np.exp(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[       3,        9,       27,       81,      243],\n       [     729,     2187,     6561,    19683,    59049],\n       [  177147,   531441,  1594323,  4782969, 14348907]], dtype=int32)"
     },
     "metadata": {},
     "execution_count": 28
    }
   ],
   "source": [
    "np.power(3,X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0.        , 0.69314718, 1.09861229, 1.38629436, 1.60943791],\n       [1.79175947, 1.94591015, 2.07944154, 2.19722458, 2.30258509],\n       [2.39789527, 2.48490665, 2.56494936, 2.63905733, 2.7080502 ]])"
     },
     "metadata": {},
     "execution_count": 29
    }
   ],
   "source": [
    "np.log(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0.        , 1.        , 1.5849625 , 2.        , 2.32192809],\n       [2.5849625 , 2.80735492, 3.        , 3.169925  , 3.32192809],\n       [3.45943162, 3.5849625 , 3.70043972, 3.80735492, 3.9068906 ]])"
     },
     "metadata": {},
     "execution_count": 30
    }
   ],
   "source": [
    "np.log2(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0.        , 0.30103   , 0.47712125, 0.60205999, 0.69897   ],\n       [0.77815125, 0.84509804, 0.90308999, 0.95424251, 1.        ],\n       [1.04139269, 1.07918125, 1.11394335, 1.14612804, 1.17609126]])"
     },
     "metadata": {},
     "execution_count": 31
    }
   ],
   "source": [
    "np.log10(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 矩阵运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0, 1],\n       [2, 3]])"
     },
     "metadata": {},
     "execution_count": 33
    }
   ],
   "source": [
    "A = np.arange(4).reshape(2, 2)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[10, 10],\n       [10, 10]])"
     },
     "metadata": {},
     "execution_count": 35
    }
   ],
   "source": [
    "B = np.full((2, 2), 10)\n",
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[10, 11],\n       [12, 13]])"
     },
     "metadata": {},
     "execution_count": 36
    }
   ],
   "source": [
    "A + B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[-10,  -9],\n       [ -8,  -7]])"
     },
     "metadata": {},
     "execution_count": 37
    }
   ],
   "source": [
    "A - B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[ 0, 10],\n       [20, 30]])"
     },
     "metadata": {},
     "execution_count": 38
    }
   ],
   "source": [
    "A * B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0. , 0.1],\n       [0.2, 0.3]])"
     },
     "metadata": {},
     "execution_count": 39
    }
   ],
   "source": [
    "A /B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[10, 10],\n       [50, 50]])"
     },
     "metadata": {},
     "execution_count": 40
    }
   ],
   "source": [
    "A.dot(B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0, 2],\n       [1, 3]])"
     },
     "metadata": {},
     "execution_count": 41
    }
   ],
   "source": [
    "A.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "C = np.full((3, 3), 666)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[666, 666, 666],\n       [666, 666, 666],\n       [666, 666, 666]])"
     },
     "metadata": {},
     "execution_count": 43
    }
   ],
   "source": [
    "C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "output_type": "error",
     "ename": "ValueError",
     "evalue": "shapes (2,2) and (3,3) not aligned: 2 (dim 1) != 3 (dim 0)",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-45-36f3f9c6ed4d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mA\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mC\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m: shapes (2,2) and (3,3) not aligned: 2 (dim 1) != 3 (dim 0)"
     ]
    }
   ],
   "source": [
    "A.dot(C)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 向量和矩阵运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "v= np.array([1,2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0, 1],\n       [2, 3]])"
     },
     "metadata": {},
     "execution_count": 47
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[1, 3],\n       [3, 5]])"
     },
     "metadata": {},
     "execution_count": 48
    }
   ],
   "source": [
    "v + A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[1, 2],\n       [1, 2]])"
     },
     "metadata": {},
     "execution_count": 52
    }
   ],
   "source": [
    "np.vstack([v] * A.shape[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[1, 3],\n       [3, 5]])"
     },
     "metadata": {},
     "execution_count": 53
    }
   ],
   "source": [
    "np.vstack([v] * A.shape[0]) + A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[1, 2],\n       [1, 2]])"
     },
     "metadata": {},
     "execution_count": 54
    }
   ],
   "source": [
    "np.tile(v, (2, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[1, 3],\n       [3, 5]])"
     },
     "metadata": {},
     "execution_count": 55
    }
   ],
   "source": [
    "np.tile(v, (2, 1)) + A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0, 2],\n       [2, 6]])"
     },
     "metadata": {},
     "execution_count": 56
    }
   ],
   "source": [
    "v * A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([2, 8])"
     },
     "metadata": {},
     "execution_count": 57
    }
   ],
   "source": [
    "A.dot(v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([4, 7])"
     },
     "metadata": {},
     "execution_count": 58
    }
   ],
   "source": [
    "v.dot(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 矩阵的逆"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0, 1],\n       [2, 3]])"
     },
     "metadata": {},
     "execution_count": 59
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[-1.5,  0.5],\n       [ 1. ,  0. ]])"
     },
     "metadata": {},
     "execution_count": 60
    }
   ],
   "source": [
    "np.linalg.inv(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "invA = np.linalg.inv(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[1., 0.],\n       [0., 1.]])"
     },
     "metadata": {},
     "execution_count": 62
    }
   ],
   "source": [
    "invA.dot(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = np.arange(16).reshape((2, 8))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[ 0,  1,  2,  3,  4,  5,  6,  7],\n       [ 8,  9, 10, 11, 12, 13, 14, 15]])"
     },
     "metadata": {},
     "execution_count": 64
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "output_type": "error",
     "ename": "LinAlgError",
     "evalue": "Last 2 dimensions of the array must be square",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mLinAlgError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-65-47889a8f1529>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlinalg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minv\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mD:\\Anaconda3\\lib\\site-packages\\numpy\\linalg\\linalg.py\u001b[0m in \u001b[0;36minv\u001b[1;34m(a)\u001b[0m\n\u001b[0;32m    544\u001b[0m     \u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mwrap\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_makearray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    545\u001b[0m     \u001b[0m_assertRankAtLeast2\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 546\u001b[1;33m     \u001b[0m_assertNdSquareness\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    547\u001b[0m     \u001b[0mt\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mresult_t\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_commonType\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    548\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Anaconda3\\lib\\site-packages\\numpy\\linalg\\linalg.py\u001b[0m in \u001b[0;36m_assertNdSquareness\u001b[1;34m(*arrays)\u001b[0m\n\u001b[0;32m    211\u001b[0m         \u001b[0mm\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mn\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    212\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mm\u001b[0m \u001b[1;33m!=\u001b[0m \u001b[0mn\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 213\u001b[1;33m             \u001b[1;32mraise\u001b[0m \u001b[0mLinAlgError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Last 2 dimensions of the array must be square'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    214\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    215\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m_assertFinite\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0marrays\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mLinAlgError\u001b[0m: Last 2 dimensions of the array must be square"
     ]
    }
   ],
   "source": [
    "np.linalg.inv(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 伪逆矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [],
   "source": [
    "pinvX = np.linalg.pinv(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[-1.35416667e-01,  5.20833333e-02],\n       [-1.01190476e-01,  4.16666667e-02],\n       [-6.69642857e-02,  3.12500000e-02],\n       [-3.27380952e-02,  2.08333333e-02],\n       [ 1.48809524e-03,  1.04166667e-02],\n       [ 3.57142857e-02, -6.93889390e-18],\n       [ 6.99404762e-02, -1.04166667e-02],\n       [ 1.04166667e-01, -2.08333333e-02]])"
     },
     "metadata": {},
     "execution_count": 68
    }
   ],
   "source": [
    "pinvX"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[ 1.00000000e+00, -2.22044605e-16],\n       [ 1.33226763e-15,  1.00000000e+00]])"
     },
     "metadata": {},
     "execution_count": 69
    }
   ],
   "source": [
    "X.dot(pinvX)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}